﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Data;

namespace Blackbird.Studio.ViewModel.Framework
{
    public class HostViewModel : WorkspaceViewModel
    {
        private WorkspaceViewModel _activeWorkspace;
        private ObservableCollection<WorkspaceViewModel> _workspaces;

        public WorkspaceViewModel ActiveWorkspace
        {
            get { return _activeWorkspace; }
            set
            {
                if (_activeWorkspace != value)
                {
                    _activeWorkspace = value;
                    InvokePropertyChanged("ActiveWorkspace");
                }
            }
        }

        public ObservableCollection<WorkspaceViewModel> Workspaces
        {
            get
            {
                if (_workspaces == null)
                {
                    _workspaces = new ObservableCollection<WorkspaceViewModel>();
                    _workspaces.CollectionChanged += OnCollectionChanged;
                }
                return _workspaces;
            }
        }

        private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null && e.NewItems.Count != 0)
                foreach (WorkspaceViewModel workspace in e.NewItems)
                {
                    workspace.RequestClose += OnWorkspaceRequestClose;
                    workspace.PropertyChanged += WorkspacePropertyChanged;
                    workspace.IsSelected = true;
                }
            if (e.OldItems != null && e.OldItems.Count != 0)
                foreach (WorkspaceViewModel workspace in e.OldItems)
                {
                    workspace.PropertyChanged -= WorkspacePropertyChanged;
                    workspace.RequestClose -= OnWorkspaceRequestClose;
                }
            if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems != null && e.NewItems.Count == 1)
            {
                var workspace = ((WorkspaceViewModel) e.NewItems[0]);

                ICollectionView collectionView = CollectionViewSource.GetDefaultView(sender);
                if (collectionView != null)
                    collectionView.MoveCurrentTo(workspace);
            }
        }

        private void WorkspacePropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsActive")
            {
                ActiveWorkspace = (WorkspaceViewModel) sender;
                foreach (WorkspaceViewModel workspace in Workspaces)
                {
                    if (workspace != ActiveWorkspace)
                    {
                        workspace.IsActive = false;
                    }
                }
            }
        }

        private void OnWorkspaceRequestClose(object sender, EventArgs e)
        {
            var workspace = sender as WorkspaceViewModel;
            if (workspace != null)
            {
                workspace.Dispose();
                Workspaces.Remove(workspace);
            }
        }
    }
}